/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Selenium;

/**
 *
 * @author Lone_Wolf
 */
import java.util.LinkedList;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Mediafire {

    private WebDriver driver;
    private WebDriver back_driver;

    public Mediafire() {
        //set location of chromedriver.exe
        System.setProperty("webdriver.chrome.driver",
                "C:\\chromedriver.exe");
        //create chromedriver
        driver = new ChromeDriver();
        initBrowser(driver);

        //create background worker
        back_driver = new ChromeDriver();
        back_driver.manage().window().setPosition(new Point(1921, 1081));
        initBrowser(back_driver);
    }

    public void quitDriver() {
        driver.quit();
    }

    public void testMediafire() {
        //set window off-screen so the user can't see it
        //driver.manage().window().setPosition(new Point(1921, 1081));

        parseDisplayedFiles(getDisplayedFiles());

        /*openUploadPanel();
        List<String> uploadList = new LinkedList<String>();
        uploadList.add("C:\\Users\\Lone_Wolf\\Downloads\\hd-pix\\pix39541.jpg");
        uploadList.add("C:\\Users\\Lone_Wolf\\Downloads\\hd-pix\\pix39648.jpg");
        addFilesToUpload(uploadList);*/

        //begin_upload();
        
        /*List<String> downloadList = new LinkedList<String>();
        downloadList.add("http://www.mediafire.com/?rqcv7lfiury4t1i");
        System.out.println(getDownloadLink(downloadList).get(0));*/

        driver.quit();
        back_driver.quit();
    }

    private void initBrowser(WebDriver d) {
        //get the MF main page
        d.get("http://www.mediafire.com");

        //login
        WebElement login_tab = d.findElement(By.id("headerlogin_tab"));
        login_tab.click();
        WebElement login_email = d.findElement(By.id("login_email"));
        login_email.sendKeys("patricksamson236@gmail.com");
        WebElement login_pass = d.findElement(By.id("login_pass"));
        login_pass.sendKeys("smarties");
        WebElement login_submit = d.findElement(By.id("submit_login"));
        //login_submit.click();
        login_submit.submit();

        //wait for everything to be loaded
        while (d.findElements(By.id("progress-total")).isEmpty()) {
            //wait until page is loaded
        }
        while (!d.findElement(By.id("progress-current")).getText().
                equals(d.findElement(By.id("progress-total")).getText())) {
            //wait for all the files element to be loaded
        }
    }

    private void openUploadPanel() {
        //attempt to upload a file
        //open the upload panel
        WebElement upload = driver.findElement(By.id("upload_to_current_folder"));
        upload = upload.findElement(By.tagName("a"));
        upload.click();

        //new method to find when animation is done
        while (driver.findElements(By.id("MediaFireGroup1_4")).isEmpty()) {
            //wait
        }
        double initPos = getUploadPanelPosition();
        while (getUploadPanelPosition() == initPos) {
            //wait
        }
        double prev_pos = initPos;
        double currentpos = getUploadPanelPosition();
        while (!(currentpos + "").endsWith(".0") || currentpos != prev_pos) {
            //System.out.println(prev_pos + " --> " + currentpos);
            prev_pos = currentpos;
            currentpos = getUploadPanelPosition();
        }
        //System.out.println(prev_pos + " --> " + currentpos);
        //now animation is done!
    }

    public void addFilesToUpload(List list) {
        List up_files = new LinkedList();
        WebElement file_input = driver.findElement(By.id("step4_html5_big_file_input_0"));
        int errors = 0;

        //add files to queue
        for (int i = 0; i < list.size(); i++) {
            try {
                file_input.sendKeys((String) list.get(i));
            } catch (Exception e) {
                errors++;
            }
        }

        WebElement upload_list = driver.findElement(By.id("step4_file_container_0"));
        up_files = upload_list.findElements(By.className("uploader_file_container"));
        while (up_files.size() != (list.size() - errors)) {
            up_files = upload_list.findElements(By.className("uploader_file_container"));
        }

        filesUploading(up_files);
    }

    private List getDisplayedFiles() {
        //get all files currently displayed
        List files = new LinkedList();
        WebElement main_list = driver.findElement(By.id("main_list"));
        files = main_list.findElements(By.tagName("li"));

        return files;
    }

    public void parseDisplayedFiles(List files) {
        System.out.println("there is " + files.size() + " elements in this location");
        for (int i = 0; i < files.size(); i++) {
            System.out.println("");
            //get file type & public/private
            System.out.println(((WebElement) files.get(i)).getAttribute("class").replace("row_container mf_filecontainer ", ""));
            //get filename
            System.out.println(((WebElement) files.get(i)).findElement(By.className("foldername")).getText());
            //get file/folder info
            System.out.println(((WebElement) files.get(i)).findElement(By.className("file_maindetails")).getText());
            //get link
            System.out.println(((WebElement) files.get(i)).findElement(By.className("filetype_column")).findElement(By.tagName("a")).getAttribute("href"));
        }

        System.out.println("\n----------------\n\n");
    }

    public void filesUploading(List up_files) {
        System.out.println(up_files.size() + " files will be uploaded");
        for (int i = 0; i < up_files.size(); i++) {
            //get filename
            System.out.println(((WebElement) up_files.get(i)).findElement(By.className("uploader_file_name")).getText());
            //get file size
            System.out.println(((WebElement) up_files.get(i)).findElement(By.className("uploader_file_size")).getText());
        }
    }

    public void begin_upload() {
        //begin upload
        WebElement begin_upload = driver.findElement(By.id("step4_begin_upload_0"));
        int MAX_TRIES_BEGIN_UPLOAD = 1;
        while (!driver.findElements(By.id("step4_begin_upload_0")).isEmpty()) {
            System.out.println("button is displayed");
            for (int i = 0; i < MAX_TRIES_BEGIN_UPLOAD; i++) {
                try {
                    begin_upload.click();
                    i = MAX_TRIES_BEGIN_UPLOAD;
                } catch (Exception e) {
                }
            }
        }
    }

    private double getUploadPanelPosition() {
        WebElement mfg = driver.findElement(By.id("MediaFireGroup1_4"));
        String pos = mfg.getAttribute("style");
        pos = pos.substring(pos.indexOf("top: ") + 5, pos.indexOf("px", pos.indexOf("top: ")));
        return (Double.parseDouble(pos));
    }

    public List<String> getDownloadLink(List list) {
        List<String> links = new LinkedList<String>();
        WebElement element;

        for (int i = 0; i < list.size(); i++) {
            //load the page
            back_driver.get((String) list.get(i));

            // Find the download button
            element = back_driver.findElement(By.className("download_link"));
            element = element.findElement(By.tagName("a"));
            links.add(element.getAttribute("href"));
        }

        return links;
    }
}
